﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net.Nntp.Cache
{
    using System;
    using System.Data.SqlServerCe;

    /// <summary>Provides the means to update outbound articles.</summary>
    internal sealed class OutboundArticleUpdater : IDisposable
    {
        private static readonly string[] articleColumns =
        {
            OutboundArticleNames.DateUtcColumn,
            OutboundArticleNames.SubjectColumn
        };

        private static readonly string[] sendStatusColumn = { OutboundArticleNames.SendStatusColumn };
        private static readonly string[] isDeletedColumn = { OutboundArticleNames.IsDeletedColumn };

        private readonly SqlCeCommand command;
        private readonly SqlCeResultSet resultSet;

        private readonly int subjectMaxLength;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>See <see cref="IDisposable.Dispose"/>.</summary>
        public void Dispose()
        {
            this.resultSet.Dispose();
            this.command.Dispose();
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal OutboundArticleUpdater(SqlCeConnection connection)
        {
            using (var articleCommandGuard =
                ScopeGuard.Create(this.command = CommandHelper.Create(connection, OutboundArticleNames.Table)))
            using (var articleResultSetGuard =
                ScopeGuard.Create(this.resultSet = this.command.ExecuteResultSet(ResultSetOptions.Updatable)))
            {
                using (var articleSchemaTable = this.resultSet.GetSchemaTable())
                {
                    this.subjectMaxLength =
                        SqlCeHelper.GetColumnSize(articleSchemaTable, OutboundArticleNames.SubjectColumn);
                }

                articleCommandGuard.Dismiss();
                articleResultSetGuard.Dismiss();
            }
        }

        internal int SubjectMaxLength
        {
            get { return this.subjectMaxLength; }
        }

        internal void Update(int articleId, DateTime? dateUtc, string subject)
        {
            this.Update(articleId, articleColumns, dateUtc, subject);
        }

        internal void Update(int articleId, SendStatus sendStatus)
        {
            this.Update(articleId, sendStatusColumn, (int)sendStatus);
        }

        internal void Update(int articleId, bool isDeleted)
        {
            this.Update(articleId, isDeletedColumn, isDeleted);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void Update(int articleId, string[] columnNames, params object[] values)
        {
            this.resultSet.Update(new object[] { articleId }, columnNames, values);
        }
    }
}
